package com.stylefeng.guns.rest.modular.wz.api.v4;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.rest.common.base.controller.ApiBaseController;
import com.stylefeng.guns.rest.common.persistence.dao.WzMapActivityMapper;
import com.stylefeng.guns.rest.common.persistence.dao.WzUserActivityMapper;
import com.stylefeng.guns.rest.common.persistence.model.WzAdvMap;
import com.stylefeng.guns.rest.common.persistence.model.WzMapActivity;
import com.stylefeng.guns.rest.common.persistence.model.WzUserActivity;
import com.stylefeng.guns.rest.domain.ParamMap;
import com.stylefeng.guns.rest.domain.Res;
import com.stylefeng.guns.rest.enums.EnWzMapActivity;
import com.stylefeng.guns.rest.modular.auth.util.JwtTokenUtil;
import com.stylefeng.guns.rest.modular.wz.api.v3.service.impl.MapActivityServiceImpl;
import com.stylefeng.guns.rest.modular.wz.cache.CacheService;
import com.stylefeng.guns.rest.modular.wz.util.wz.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @program: wz_dev_app_shop
 * @description:
 * @author: Chenchao
 * @create: 2019-02-14 10:38
 **/
@RestController
@RequestMapping("/wz/api/v4/mapActivity/")
public class WzMapActivityApiV4 extends ApiBaseController {
    @Autowired
    private MapActivityServiceImpl mapActivityService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private WzMapActivityMapper mapActivityMapper;
    @Autowired
    private CacheService cacheService;
    @Autowired
    private WzUserActivityMapper userActivityMapper;
    /**
     * 活动列表
     */
    @PostMapping("list")
    public Res list(@RequestBody ParamMap paramMap){
        Long userCode = jwtTokenUtil.getUserCodeFromToken();
        String switchAdCode = paramMap.getStrValid("switchAdCode");
        if(StringUtils.isBlank(switchAdCode)){
            switchAdCode= paramMap.getStrMisValid("adCode");
        }
        Wrapper e = new EntityWrapper<WzMapActivity>().eq("status", EnWzMapActivity.STATUS_RELEASE.getIndex()).like("ad_code",switchAdCode).ge("end_time", new Date()).gt("surplus_number",0);
           //全部活动
        Integer advMapCategoryFirst = paramMap.getIntValid("advMapCategoryFirst");//0:全部
        if(advMapCategoryFirst != 0){
            e.eq("adv_map_category_first", advMapCategoryFirst);
        }
        int pageNum = paramMap.getIntMisValid("pageNum");
        int pageSize = paramMap.getIntMisValid("pageSize");
        List<Map<String, Object>> advMapIds=mapActivityMapper.selectMapsPage(new Page<>(pageNum, pageSize),e.setSqlSelect("adv_map_id As advMapId").eq("status", EnWzMapActivity.STATUS_RELEASE.getIndex()).groupBy("adv_map_id"));
        List advMapList=new ArrayList();
        for(Map advMapIdMap:advMapIds){
            Map temp=new HashMap();
            WzAdvMap advMap = cacheService.advMap(Long.parseLong(advMapIdMap.get("advMapId").toString()));
            temp.put("name",advMap.getName());
            temp.put("logo",advMap.getLogo());
            temp.put("longitude", advMap.getLongitude());
            temp.put("latitude", advMap.getLatitude());
            temp.put("advMapId", advMap.getAdvMapId());
            String advMapId=advMapIdMap.get("advMapId").toString();
            String mapActivityListSql = "map_activity_id As mapActivityId,subtype,join_number As joinNumber,need_share_number As needShareNumber,benefit,begin_time As beginTime,end_time As endTime,pic,usetype,jointype,total_number As totalNumber";
            List<Map<String, Object>> mapActivityList = mapActivityMapper.selectMaps(new EntityWrapper<WzMapActivity>().eq("status", EnWzMapActivity.STATUS_RELEASE.getIndex()).ge("end_time", new Date()).gt("surplus_number",0).eq("adv_map_id",advMapId).setSqlSelect(mapActivityListSql));//活动列表
            temp.put("mapActivityList",mapActivityList);
            advMapList.add(temp);
        }
        //5天内我参加的活动和一人只能参加一次的活动
        Date date = DateUtil.getDateBefore(new Date(), 5);
        String userActivityListSql = "user_map_activity_id As userMapActivityId,map_activity_id As mapActivityId,status,create_time As createTime,share_number As shareNumber,end_time As endTime";
        //每人每天可领一次
        List<Map<String, Object>> userActivityList = userActivityMapper.selectMaps(new EntityWrapper<WzUserActivity>().setSqlSelect(userActivityListSql).eq("user_code", userCode).eq("usetype", EnWzMapActivity.TYPE_DAILY.getIndex()).ge("create_time", date));
        //每人只可领取一次
        List<Map<String, Object>> onlyOnceActivityList = userActivityMapper.selectMaps(new EntityWrapper<WzUserActivity>().setSqlSelect(userActivityListSql).eq("user_code", userCode).eq("usetype", EnWzMapActivity.TYPE_ONLYONCE.getIndex()).ge("end_time", new Date()));
        for (Map<String, Object> map : onlyOnceActivityList) {
            userActivityList.add(map);
        }
        paramMap.put("advMapList", advMapList);
        paramMap.put("userActivityList", userActivityList);
        return Res.success(paramMap);
    }
}

